package com.aifast.core.view;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.hutool.core.util.StrUtil;
import com.aifast.common.global.Const;
import com.aifast.common.util.AiUtil;
import com.jfinal.template.Engine;
import com.jfinal.template.ext.spring.JFinalViewResolver;
import com.jfinal.template.source.ClassPathSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author fujian
 * @since 2022/1/13 11:33
 */
@Configuration
public class ViewConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/bucket/**").addResourceLocations(StrUtil.join("", "file:", Const.CURRTPATH, "bucket/"));
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/images/favicon.ico");
        registry.addResourceHandler("/view/images/**").addResourceLocations(StrUtil.join("", "file:", Const.CURRTPATH, "images/"));
        registry.addResourceHandler("/view/aiim/image/**").addResourceLocations(StrUtil.join("", "file:", Const.CURRTPATH, "aiim/images/"));
        registry.addResourceHandler("/view/aiim/file/**").addResourceLocations(StrUtil.join("", "file:", Const.CURRTPATH, "aiim/files/"));
//        super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new SaRouteInterceptor()).addPathPatterns("/**").excludePathPatterns(AiUtil.getExcludePathPatterns());
        registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**/**").excludePathPatterns(AiUtil.getExcludePathPatterns());
        registry.addInterceptor(new AiIdempotentInterceptor()).addPathPatterns("/**");
//        super.addInterceptors(registry);
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");/*首页*/
//        super.addViewControllers(registry);
    }

    @Bean(name = "jfinalViewResolver")
    public JFinalViewResolver getJfinalViewResolver(){
        JFinalViewResolver jfr = new JFinalViewResolver();

        /**热加载*/
        jfr.setDevMode(true);
        jfr.setSourceFactory(new ClassPathSourceFactory());
        jfr.setPrefix("/templates/");
        jfr.setSuffix(".html");
        jfr.setContentType("text/html;charset=utf-8");
        jfr.setCache(true);
        jfr.setOrder(0);
        Engine.setFastMode(true);

        return jfr;
    }

}
